#include "pathutil.h"

#include <QCoreApplication>
#include <QStandardPaths>

QString PathUtil::exeDir()
{
    return QCoreApplication::applicationDirPath() + QDir::separator();
}

QString PathUtil::desktopDir()
{
    return QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + QDir::separator();
}

QString PathUtil::picturesDir()
{
    return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + QDir::separator();
}

QString PathUtil::genericDataDir()
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QDir::separator();
}

QString PathUtil::appDataDir()
{
    return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + QDir::separator();
}

QString PathUtil::appDataSubDir(const QString &subDir)
{
    QString path = appDataDir() + subDir;
    mkDir(path);
    return path + QDir::separator();
}

QString PathUtil::tempDir()
{
#ifdef Q_OS_WIN32
    return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + QDir::separator();
#else
    return "/tmp/";
#endif
}

QString PathUtil::logDir()
{
    return appDataSubDir("logs");
}

QString PathUtil::imageDir()
{
    return appDataSubDir("images");
}

QString PathUtil::config()
{
    return appDataSubDir("config") + "config.ini";
}

QString PathUtil::database()
{
    return appDataSubDir("config") + "config.db";
}

bool PathUtil::mkDir(const QString &dirPath)
{
    QDir dir(dirPath);
    if (!dir.exists())
    {
        dir.mkpath(dirPath);
    }
    return true;
}

QString PathUtil::combine(const QString &dirPath, const QString &fileName)
{
    QDir dir(dirPath);
    return dir.absoluteFilePath(fileName);
}

QString PathUtil::getFileName(const QString &path)
{
    QFileInfo fileInfo(path);
    return fileInfo.fileName();
}

QString PathUtil::getDirectoryName(const QString &path)
{
    QFileInfo fileInfo(path);
    return fileInfo.path();
}

QString PathUtil::getExtension(const QString &path)
{
    QFileInfo fileInfo(path);
    return "." + fileInfo.suffix();
}

QString PathUtil::getFileNameWithoutExtension(const QString &path)
{
    QFileInfo fileInfo(path);
    return fileInfo.completeBaseName();
}
